﻿namespace DCHealthCheck.MvcWeb.Classes.DataServices {

    #region Usings
    using DCHealthCheck.Common;
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Threading;
    using System.Web; 
    #endregion

    public class PendingReplicationsDataService : BaseDataService, IPendingReplicationsDataService {

        #region Members
        private readonly IRepository<PendingReplication, IEntityManager> PendingReplicationsRepository;

        private readonly Expression<Func<PendingReplication, object>>[] PendingReplicationsIncludedEntities;
        #endregion

        #region Constructor
        public PendingReplicationsDataService(
            IRepository<DomainController, IEntityManager> dcRepository,
            IRepository<PendingReplication, IEntityManager> pendingReplicationsRepository) : base(dcRepository) {

            this.PendingReplicationsRepository = pendingReplicationsRepository;
            this.PendingReplicationsIncludedEntities = new Expression<Func<PendingReplication, object>>[] {
                (x) => x.SystemInfo,
                (x) => x.SystemInfo.DomainController
            };
        }
        #endregion

        #region Methods

        private IQueryable<PendingReplication> GetFilteredPendingReplications(PendingReplicationsViewModel viewModel) {

            var pendingReplications = this.PendingReplicationsRepository
                .Get(x => x.SystemInfo.Id == viewModel.SystemInfoId);

            return pendingReplications;
        }

        public void ProcessData(PendingReplicationsViewModel viewModel) {
            Global.DefaultLog.DebugFormat("[ThreadId: {0}] {1}", Thread.CurrentThread.ManagedThreadId, Extensions.CurrentMethodName());

            try {
                viewModel.Initialize();
                var pendingReplications = this.GetFilteredPendingReplications(viewModel);
                viewModel.DisplayTotalResults = pendingReplications.Count();

                #region Skip count, take, and sorting
                if (!string.IsNullOrWhiteSpace(viewModel.SortColumn)) {
                    if (viewModel.CurrentSortDirection == System.Web.Helpers.SortDirection.Ascending) {
                        viewModel.PendingReplications = pendingReplications
                            .OrderBy(viewModel.SortColumn)
                            .Skip(viewModel.SkipCount.Value)
                            .Take(viewModel.TakeCount.Value)
                            .ToList();
                    }
                    else {
                        viewModel.PendingReplications = pendingReplications
                            .OrderByDescending(viewModel.SortColumn)
                            .Skip(viewModel.SkipCount.Value)
                            .Take(viewModel.TakeCount.Value)
                            .ToList();
                    }
                }
                else {
                    viewModel.PendingReplications = pendingReplications
                        .OrderBy(x => x.SystemInfo.DomainController.SamAccountName)
                        .Skip(viewModel.SkipCount.Value)
                        .Take(viewModel.TakeCount.Value)
                        .ToList();
                }
                #endregion

                viewModel.GetPageCounts();
            }
            catch (Exception e) {
                Global.DefaultLog.DebugFormat("[ThreadId: {0}] {1} {2}", Thread.CurrentThread.ManagedThreadId, Extensions.CurrentMethodName(), e.VerboseExceptionString());
            }
        }
        #endregion
    }
}